package com.qf.factory;

import java.io.IOException;
import java.util.Properties;

public class MyFactory {
    //用来读取properties配置文件内容
    private Properties properties = new Properties();

    //无参构造
    public MyFactory(){

    }

    public MyFactory(String config) throws IOException {
        properties.load(MyFactory.class.getResourceAsStream(config));
    }
    /**
     * 根据传入的javabean的名字获取javabean的实例化对象
     * @param beanName java类的名字
     * @return
     */
    public Object getBean(String beanName) throws Exception {
        //获取类的路径
        String classpath = properties.getProperty(beanName);

        if (classpath != null) {
            //通过反射加载类对象
            Class<?> aClass = Class.forName(classpath);

            //通过反射获取类的实例化对象
            Object obj = aClass.newInstance();
            return obj;
        }

        //返回
        return null;
    }
}
